//
//  YTLanguageChangeViewController.m
//  YiTuDriverClient
//
//  Created by 石少庸 on 2017/6/7.
//  Copyright © 2017年 Yitu. All rights reserved.
//

#import "YTLanguageChangeViewController.h"
#import "YTServiceLanguageModel.h"

@interface YTLanguageChangeViewController ()
@property (weak, nonatomic) IBOutlet UIView *serviceView;
@property (strong, nonatomic) NSMutableArray<YTServiceLanguageModel *> *dataArray;
@property (strong, nonatomic) NSMutableSet *languageString;
@property (strong, nonatomic) NSMutableSet *languageID;
@end

@implementation YTLanguageChangeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _dataArray = [NSMutableArray array];
    _languageID = [NSMutableSet set];
    _languageString = [NSMutableSet set];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [[YTNetManager manager] postWithURL:@"car/getLanguage" parameters:dict success:^(id response) {
        NSDictionary *data = response[@"responseData"];
        _dataArray = [[NSArray yy_modelArrayWithClass:[YTServiceLanguageModel class] json:data[@"languageList"]] mutableCopy];
        for (NSInteger i = 0; i < _dataArray.count; i++) {
            [self addButtonWithIndex:i title:_dataArray[i].name];
        }
    } failure:^(NSError *error) {
        
    }];
}

- (void)addButtonWithIndex:(NSInteger)index title:(NSString *)title {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:title forState:UIControlStateNormal];
    [button.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [button setTitleColor:kBlackColor forState:UIControlStateNormal];
    [button setTitleColor:kMainBlueColor forState:UIControlStateSelected];
    [button setBackgroundImage:[UIImage imageNamed:@"public_button_check"] forState:UIControlStateSelected];
    button.layer.cornerRadius = 3.0;
    button.layer.borderWidth = 1.0;
    button.layer.borderColor = kGrayColor.CGColor;
    [_serviceView addSubview:button];
    CGFloat width = (kScreenWidth-50)/3;
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(width, 35));
        make.left.mas_equalTo(15 + index%3 * (width + 10));
        make.top.mas_equalTo(15 + index/3 * 44);
    }];
    @weakify(self);
    [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(UIButton *btn) {
        @strongify(self);
        btn.selected = !btn.selected;
        btn.layer.borderWidth = btn.selected ? 0.0 : 1.0;
        btn.layer.borderColor = btn.selected ? nil : kGrayColor.CGColor;
        if (btn.selected) {
            [self.languageID addObject:self.dataArray[index].language_id];
            [self.languageString addObject:title];
        } else {
            [self.languageID removeObject:self.dataArray[index].language_id];
            [self.languageString removeObject:title];
        }
    }];;
}

- (IBAction)selectedCompletionAction:(UIBarButtonItem *)sender {
    if (_selectedCallBack) {
        NSString *nameStr = [NSString string];
        NSString *idStr = [NSString string];
        for (NSString *str in self.languageString) {
            nameStr = [nameStr stringByAppendingString:[NSString stringWithFormat:@", %@", str]];
        }
        for (NSString *str in self.languageID) {
            idStr = [idStr stringByAppendingString:[NSString stringWithFormat:@", %@", str]];
        }
        
        [_selectedCallBack sendNext:RACTuplePack([nameStr substringFromIndex:2], [idStr substringFromIndex:2])];
        [_selectedCallBack sendCompleted];
        
        [self.navigationController popViewControllerAnimated:YES];
    }
}

@end
